﻿using System.Collections;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Web.UI.WebControls;
using System;
using Model;
using Service;
using Parameter = Model.Parameter;


public class Utility
{
    private static int _RowToDisplay;
    private Color _RowAlternativeColor = ColorTranslator.FromHtml("#E8F2FD");
    private Color _RowColor = Color.White;
    public static Color GetRemarkableHightLightColor ()
    {
        return Color.Red;
    }

    public  Color GetSelectedRowColor()
    {        
        return Color.FromArgb(205,255,204);
        // code #cdffcc 
    }
    public  Color RowColor
    {
        get
        {
            return _RowColor;
        }
        
    }
    public Color RowAlternativeColor
    {
        get
        {
            return _RowAlternativeColor;
        }
    }

    public static int RowToDisplay
    {
        get
        {
            return _RowToDisplay;
        }
        set
        {
            value = _RowToDisplay;
        }

    }
    public void SetSelectedColorToGridView(GridViewCommandEventArgs e, GridView gridview)
    {
        if (e.CommandName.Equals("Select"))
        {
            int rowIndex = Convert.ToInt32(e.CommandArgument);
            for (int i = 0; i < gridview.Rows.Count; i++)
            if (i != rowIndex) gridview.Rows[i].BackColor = (i%2)== 0 ? RowColor:RowAlternativeColor;
            GridViewRow selectedRow = gridview.Rows[rowIndex];
            selectedRow.BackColor = GetSelectedRowColor();
        }

    }
    public void SetRowToDisplay(GridView gridView,object rowDisplay)
    {
        if (rowDisplay != null)
            gridView.PageSize = ConvertToInteger(rowDisplay);
    }
    public int ConvertToInteger(object obj)
    {
        int i = 0;
        if(obj == null) return 0;
        if( int.TryParse(obj.ToString(), out i)) return int.Parse(obj.ToString());
        return 0;

    }
    public bool verifyTimeFormat( string time)
    {
        string[] format = time.Split(':');
        string string2;

        try
        {
            string2 = format[1];

            if (format[1] == "")
            {
                //format[1] = "00";
                string2 = "00";
            }
        }
        catch
        {
            //format[1] = "00";
            string2 = "00";
            time += ":" + string2;
        }

        try
        {
            int format1 = Convert.ToInt32(format[0]);

            if ((0 <= format1) && (format1 <= 24))
            {
                try
                {
                    int format2 = System.Convert.ToInt32(string2);

                    if (format1 == 24)
                    {
                        if (format2 == 0)
                        {
                           
                            return true;
                        }
                       
                        return false;
                        
                    }
                    else
                    {
                       
                        if ((0 <= format2) && (format2 <= 59))
                        {
                           
                            return true;
                        }
                        return false;
                    }
                    //return true;
                }
                catch (FormatException)
                {
                  
                    return false;
                }
            }
            else
            {
                
                return false;
            }
        }
        catch (FormatException)
        {
            
            return false;
        }
    }


    public DateTime ConvertToCustomizedFormat(object obj)
    {
        DateTime i = new DateTime();
        if (DateTime.TryParse(obj.ToString(), new CultureInfo("en-US", true), DateTimeStyles.None, out i))
            return DateTime.Parse(obj.ToString(), new CultureInfo("en-US", true));
        return DateTime.Parse(Resources.Resource.EmptylDateTime, new CultureInfo("en-US", true)); 

    }
    public String ConvertToDisplayDateString(object obj)
    {
        DateTime date = new  DateTime();
        date = DateTime.Parse(obj.ToString());
        if (!date.ToString(Resources.Resource.DateEnglishFormat).Equals(Resources.Resource.EmptylDateTime))
        {
            return date.ToString(Resources.Resource.DateEnglishFormat);
        }
        return string.Empty;

    }
    public string ConvertToStringDateTimeFrenchFormat(object obj)
    {
        DateTime i = new DateTime();
        
        if (DateTime.TryParse(obj.ToString(), out i))
            return (DateTime.Parse(obj.ToString()).ToString(Resources.Resource.DateFrenchFormat));
        return Resources.Resource.EmptylDateTime;
    }
   
    public bool TestPermissions(string permissionId)
    {

        Hashtable hashtablePermission = new Hashtable();
        hashtablePermission = Model.Parameter.hashTablePermission;
        if(hashtablePermission != null)
            return hashtablePermission.ContainsKey(permissionId);
        return false;
    }
    public bool LoadPermissionUser(string domainName)
    {
        
        DirectServiceLogin loginService = new DirectServiceLogin();
        ReferenceDataService referenceDataService = new ReferenceDataService();
        Employee employee  =referenceDataService.GetEmployeeByDomainName(domainName);
        if (employee.EmployeeId == 0) return false;
        
            
        
            
        Employee.USERID = employee.EmployeeId;
        Employee.GROUPID = employee.GroupId;
       
        
        DataTable dataTable = new DataTable();
        dataTable = loginService.GetPermission(Employee.GROUPID);
        int ro = dataTable.Rows.Count;
        Parameter.hashTablePermission = new UitilityService().GetUserPermissions(dataTable);
        return true;
    }
   
}
